package com.ruoyi.web.controller.mdm;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mdm.domain.SearchConditionInfo;
import com.ruoyi.mdm.domain.dto.SkuMasterDto;
import com.ruoyi.mdm.service.IMasterDataService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletResponse;

import java.util.List;

/**
 * MDM数据服务Controller
 *
 * @author shiseido
 * @date 2025-03-01
 */
@RestController
@RequestMapping("/mdm/masterData")
public class DataInfoController extends BaseController {

    @Autowired
    private IMasterDataService masterDataService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询数据服务数据列表
     */
    @ApiOperation("查询数据服务数据列表")
//    @PreAuthorize("@ss.hasPermi('mdm:masterData:list')")
    @GetMapping("/list")
    public Object list(SearchConditionInfo searchConditionInfo) {

        // 服务渠道
        String channelCode = configService.selectConfigByKey("sys.channelCode");
        if (StringUtils.isNotEmpty(channelCode)) {
            searchConditionInfo.setChannelCode(channelCode);
        }

//        // 销售渠道不为空时 转换为list对象
//        if (StringUtils.isNotEmpty(searchConditionInfo.getSalesChannelCode())) {
//            List<String> salesChannelCodeList = Arrays.asList(searchConditionInfo.getSalesChannelCode().split(","));
//            searchConditionInfo.setSalesChannelCodeList(salesChannelCodeList);
//        }

        // 检索条件
        if ("mysql".equals(searchConditionInfo.getSearchType())) {
            startPage();
            List<?> list = masterDataService.selectMasterData(searchConditionInfo);
            return getDataTable(list);
        } else {
            return masterDataService.searchMasterData(searchConditionInfo);
        }

    }

    /**
     * 导出sku管理列表
     */
    @ApiOperation("导出产品管理列表")
    @PreAuthorize("@ss.hasPermi('mdm:masterData:export')")
    @Log(title = "产品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SearchConditionInfo searchConditionInfo) {

        // 最大10000条数据
        searchConditionInfo.setPageNum(1);
        searchConditionInfo.setPageSize(20000);

        // 服务渠道
        String channelCode = configService.selectConfigByKey("sys.channelCode");
        if (StringUtils.isNotEmpty(channelCode)) {
            searchConditionInfo.setChannelCode(channelCode);
        }

//        // 销售渠道不为空时 转换为list对象
//        if (StringUtils.isNotEmpty(searchConditionInfo.getSalesChannelCode())) {
//            List<String> salesChannelCodeList = Arrays.asList(searchConditionInfo.getSalesChannelCode().split(","));
//            searchConditionInfo.setSalesChannelCodeList(salesChannelCodeList);
//        }

        TableDataInfo rspData = (TableDataInfo) masterDataService.searchMasterData(searchConditionInfo);
        List<SkuMasterDto> skuMasterDtoList = (List<SkuMasterDto>) rspData.getRows();
        ExcelUtil<SkuMasterDto> util = new ExcelUtil<SkuMasterDto>(SkuMasterDto.class);
        util.exportExcel(response, skuMasterDtoList, "产品管理数据");
    }
}
